// resume after dead
import {GuiControllerOpt, GuiId, TimeControllerOpt} from "../controller/model/ControllerOpt";
import {TimeController} from "../controller/TimeController";
import {GlobalEnv} from "../utils/GlobalEnv";
import Configer from "../frame/apiModel/config/Configer";
import {ConfigKey, ResumeType} from "../constant/ConfigConstant";
import {UnitController} from "../controller/UnitController";
import {TriggerFactory} from "../frame/TriggerFactory";
import {UnitModel} from "../frame/apiModel/adapterModel/model3/UnitModel";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {GuiController, OptType} from "../controller/GuiController";
import {CodeUtil} from "../frame/codeAdapter/CodeUtil";
import {UnitDataKey} from "../frame/dataSource/DataSource";
import {DataRouterUtil} from "../utils/router/DataRouterUtil";
import {Logger} from "../frame/apiModel/config/Logger";

export function resumeHeroTrigger() {
    const resumeTypes: ResumeType[] = Configer.getConfig(ConfigKey.resumeTypes);
    const isAutoResume = CodeUtil.inArr(resumeTypes, ResumeType.autoResume);
    const isUIResume = CodeUtil.inArr(resumeTypes, ResumeType.UIResume);
    if (isAutoResume) {
        const deadCallBack = function (this: void, killer: UnitModel, killed: UnitModel, killerPlayer: PlayerModel, killedPlayer: PlayerModel) {
            const deadUnit = killed;
            const isInHeroGroup = GlobalEnv.isInHeroGroup(deadUnit);
            if (isInHeroGroup) {
                const unitData = DataRouterUtil.getUnitDataByUnit(deadUnit);
                Logger.toastProduct(`${unitData.Name} 已死亡`)
                if (unitData?.resumeTime) {
                    deadUnit.data[UnitDataKey.resumeTime] = unitData.resumeTime;
                } else {
                    deadUnit.data[UnitDataKey.resumeTime] = -1;
                }

                const showDialog = Configer.getConfig(ConfigKey.showTimeDialog);
                if (showDialog > 0) {
                    const calllBack = () => {
                    }
                    const timerOpt = new TimeControllerOpt(`${deadUnit.name} 复活`, calllBack, 1);
                    const timeControl = TimeController.getInstance().startTimer({time: deadUnit.data[UnitDataKey.resumeTime], opt: timerOpt});
                }
            }
            const isInPet = UnitController.getInstance().isInPets(deadUnit);
            if (isInPet) {
                const unitData = DataRouterUtil.getUnitDataByUnit(deadUnit);
                Logger.toastProduct(`宠物 ${unitData.Name} 已死亡`, killedPlayer)
                if (unitData?.resumeTime) {
                    deadUnit.data[UnitDataKey.resumeTime] = unitData.resumeTime;
                } else {
                    deadUnit.data[UnitDataKey.resumeTime] = -1;
                }
            }
        }
        TriggerFactory.getInstance().registerUnitDead(deadCallBack)
    }
    if (isUIResume) {
        const deadCallBack = function (this: void, killer: UnitModel, killed: UnitModel, killerPlayer: PlayerModel, killedPlayer: PlayerModel) {
            const deadUnit = killed;
            const isInHeroGroup = GlobalEnv.isInHeroGroup(deadUnit);
            if (!isInHeroGroup) {
                return;
            }
            if (killedPlayer.isNotEndPlaying()) {
                const request = {  };
                const opt = new GuiControllerOpt(GuiId.resumeBar, request);
                GuiController.getInstance().showGui(opt, killedPlayer, OptType.open);
            }
        }
        TriggerFactory.getInstance().registerUnitDead(deadCallBack)
    }

}